package edu.uta.BillMeApp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import edu.BillME.datapars.HttpConnection;
import edu.BillME.gui.HistoryTabs;
import edu.BillME.gui.RecentHistory;
import edu.uta.BillMeApp.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//import android.widget.TextView;
import android.widget.Toast;
/* @auther Siddharth Patel & Susan Foster */
public class TableOfCont extends Activity {
	JSONArray jArray;
	String getWN;
	final static String URL="http://omega.uta.edu/~smp3753/tableofc.php";
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tableofcont);
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
       
        TextView welcome,billtxt,paymenttxt;
     
        welcome=(TextView)findViewById(R.id.page1user);
        billtxt=(TextView)findViewById(R.id.billstext);
        paymenttxt=(TextView)findViewById(R.id.paymentstxt);
        
        Button Add_Friend = (Button) findViewById(R.id.add_friend);
        Button Itemized_Bill = (Button) findViewById(R.id.itemized_bill);
        Button Recent_Activity = (Button) findViewById(R.id.recent_activity);
        Button Account_History= (Button) findViewById(R.id.account_history);
        
        Bundle gotBundle=getIntent().getExtras();
        getWN=gotBundle.getString("wN");
        String s="Hello ";
        postParameters.add(new BasicNameValuePair("firstname", getWN));
        String bil="Your bill account: ";
        String pay="Your payment account: ";
        welcome.setText(s.concat(getWN.concat("!!")));
        
        //paring data
        try{
     	  
            jArray = new JSONArray(HttpConnection.executeHttpPost(URL,postParameters));
            Log.d("connection ","connection at TOC");
            for(Integer i=0; i< jArray.length(); i++){
                try{
                    //Get My JSONObject and grab the String Value that I want.
                    String obj = jArray.getJSONObject(i).getString("Bills");
                    String obj1 = jArray.getJSONObject(i).getString("Payments");

                    //Add the string to the list                  
                    billtxt.setText(bil.concat(obj));
                    paymenttxt.setText(pay.concat(obj1));
                      
                }catch(JSONException e){
                 Toast.makeText(getBaseContext(), "Not Found" ,Toast.LENGTH_LONG).show();
                }
                                 
            }

        }catch (Exception e1) {
         Log.e("log_tag", "Error final1212 "+e1.toString());
         }

        Add_Friend.setOnClickListener(new View.OnClickListener() {
			
	//		@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("wN",getWN);
				Intent Intentaddfriend = new Intent(view.getContext(),AddFriend.class);
				Intentaddfriend.putExtras(bundle);
				startActivity(Intentaddfriend);
			}
		});
 
   Itemized_Bill.setOnClickListener(new View.OnClickListener() {
		
	//	@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			Bundle bundle = new Bundle();
			bundle.putString("wN",getWN);
			Intent IntentBill = new Intent(view.getContext(),ItemizedBill.class);
			IntentBill.putExtras(bundle);
			startActivity(IntentBill);
		}
	});
    
   Account_History.setOnClickListener(new View.OnClickListener() {
		
	//	@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			Bundle bundle = new Bundle();
			bundle.putString("wN",getWN);
			Intent IntentHistory = new Intent(view.getContext(),HistoryTabs.class);
			IntentHistory.putExtras(bundle);
			startActivity(IntentHistory);
		}
	});

   Recent_Activity.setOnClickListener(new View.OnClickListener() {
		
		//	@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("wN",getWN);
				//Intent IntentHistory = new Intent(view.getContext(),History.class);
				Intent IntentHistory = new Intent(view.getContext(),RecentHistory.class);
				IntentHistory.putExtras(bundle);
				startActivity(IntentHistory);
			}
		});

}
}